// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2009-10-26 21:57:27


// Source File Name:   Method.java

package com.vhly.classfile;

import com.vhly.classfile.util.CPTool;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

// Referenced classes of package com.vhly.classfile:
//            CPUtf8, Code, Exceptions, Deprecated, 
//            Unknown, Attribute, Debug, ConstantPool

public class Method {

    private int access;
    private int name_index;
    private int sig_index;
    private int attribute_count;
    private Vector atts;
    private int code_index;
    private int exception_index;
    private int codename_index;
    private int exceptionname_index;
    private int deprecated_index;
    private int dapercatedname_index;
    private ConstantPool cp;
    private boolean bDeprecated;

    public static Method createMethod(ConstantPool constantpool, String name, String type) {
        Method ret;
        int nindex = CPTool.addUTF8(constantpool,name);
        int tindex = CPTool.addUTF8(constantpool,type);
        ret = new Method(AccessFlag.ACC_PUBLIC, nindex, tindex, constantpool);
        return ret;
    }


    public Method(ConstantPool constantpool) {
        access = 1;
        name_index = 0;
        sig_index = 0;
        attribute_count = 0;
        atts = new Vector();
        cp = constantpool;
        bDeprecated = false;
    }

    public Method(int acc, int nIndex, int tIndex, ConstantPool constantpool) {
        access = acc;
        name_index = nIndex;
        sig_index = tIndex;
        attribute_count = 0;
        atts = new Vector();
        cp = constantpool;
        bDeprecated = false;
    }

    public int getAttributeCount() {
        return attribute_count;
    }

    public void setAttributeCount(int s) {
        attribute_count = s;
    }

    public int getDapercatedNameIndex() {
        return dapercatedname_index;
    }

    public void setDapercatedNameIndex(int index) {
        dapercatedname_index = index;
    }

    public void read(DataInputStream datainputstream)
            throws IOException {
        if (Debug.isEnabled())
            System.out.println("Read Method start");
        access = datainputstream.readUnsignedShort();
        if (Debug.isEnabled())
            System.out.println((new StringBuilder()).append("Read Method Access:0x").append(Integer.toHexString(access)).toString());
        name_index = datainputstream.readUnsignedShort();
        if (Debug.isEnabled())
            System.out.println((new StringBuilder()).append("Read Method NameIndex:0x").append(Integer.toHexString(name_index)).toString());
        sig_index = datainputstream.readUnsignedShort();
        if (Debug.isEnabled())
            System.out.println((new StringBuilder()).append("Read Method SigIndex:0x").append(Integer.toHexString(sig_index)).toString());
        attribute_count = datainputstream.readUnsignedShort();
        if (Debug.isEnabled())
            System.out.println((new StringBuilder()).append("Read Method Attribute Count:0x").append(Integer.toHexString(attribute_count)).toString());
        if (Debug.isEnabled())
            System.out.println((new StringBuilder()).append("Read Attribute count :").append(attribute_count).toString());
        if (attribute_count != 0) {
            for (int i = 0; i < attribute_count; i++) {
                int j = datainputstream.readUnsignedShort();
                if (Debug.isEnabled())
                    System.out.println((new StringBuilder()).append(" Attribute Name Index:").append(j).toString());
                CPUtf8 cputf8 = (CPUtf8) cp.getCPEntryAtIndex(j);
                String s = cputf8.getString();
                s = s.trim();
                if (s.equals("Code")) {
                    Code code = new Code(cp);
                    code.setNameIndex(j);
                    code.read(datainputstream, false);
                    atts.add(code);
                    code_index = i;
                    codename_index = j;
                } else if (s.equals("Exceptions")) {
                    Exceptions exceptions = new Exceptions();
                    exceptions.setNameIndex(j);
                    exceptions.read(datainputstream, false);
                    atts.add(exceptions);
                    exception_index = i;
                    exceptionname_index = j;
                } else if (s.equals("Deprecated")) {
                    bDeprecated = true;
                    Deprecated deprecated = new Deprecated();
                    deprecated.setNameIndex(j);
                    deprecated.read(datainputstream, false);
                    atts.add(deprecated);
                    deprecated_index = i;
                    dapercatedname_index = j;
                } else {
                    Unknown unknown = new Unknown();
                    unknown.setNameIndex(j);
                    unknown.read(datainputstream, false);
                    atts.add(unknown);
                }
                if (Debug.isEnabled())
                    System.out.println((new StringBuilder()).append("Attribute ").append(i).toString());
            }

        }
    }

    public void write(DataOutputStream dataoutputstream)
            throws IOException {
        dataoutputstream.writeShort(access);
        if (Debug.isEnabled())
            System.out.println((new StringBuilder()).append("Write Method Access:").append(access).toString());
        dataoutputstream.writeShort(name_index);
        if (Debug.isEnabled())
            System.out.println((new StringBuilder()).append("Write Method Name_index:").append(name_index).toString());
        dataoutputstream.writeShort(sig_index);
        if (Debug.isEnabled())
            System.out.println((new StringBuilder()).append("Write Method sig_index:").append(sig_index).toString());
        attribute_count = atts.size();
        dataoutputstream.writeShort(attribute_count);
        if (Debug.isEnabled())
            System.out.println((new StringBuilder()).append("Write Method attribute_count:").append(attribute_count).toString());
        for (int i = 0; i < atts.size(); i++) {
            Attribute attribute = (Attribute) atts.elementAt(i);
            if (Debug.isEnabled()) {
                System.out.println((new StringBuilder()).append("Write Attribute Name_index:").append(attribute.getNameIndex()).toString());
                System.out.println((new StringBuilder()).append("Write Attribute data_len:").append(attribute.getDataLen()).toString());
                System.out.println((new StringBuilder()).append("Write M's Attribute ").append(i).toString());
            }
            attribute.write(dataoutputstream);
        }

    }

    public int getNameIndex() {
        return name_index;
    }

    public void setNameIndex(int i) {
        name_index = i;
    }

    public String getName() {
        CPUtf8 cputf8 = (CPUtf8) cp.getCPEntryAtIndex(name_index);
        String s = cputf8.getString();
        return s;
    }

    public void setName(String s) {
        CPUtf8 cputf8 = (CPUtf8) cp.getCPEntryAtIndex(name_index);
        cputf8.setString(s);
    }

    public int getAccess() {
        return access;
    }

    public void setAccess(int i) {
        access = i;
    }

    public int getSigIndex() {
        return sig_index;
    }

    public void setSigIndex(int i) {
        sig_index = i;
    }

    public String getSig() {
        CPUtf8 cputf8 = (CPUtf8) cp.getCPEntryAtIndex(sig_index);
        String s = cputf8.getString();
        return s;
    }

    public void setSig(String s) {
        CPUtf8 cputf8 = (CPUtf8) cp.getCPEntryAtIndex(sig_index);
        cputf8.setString(s);
    }

    public void setCodeIndex(int i) {
        code_index = i;
    }

    public int getCodeIndex() {
        return code_index;
    }

    public int getCodeNameIndex() {
        return codename_index;
    }

    public void setCodeNameIndex(int i) {
        Code code = getCode();
        code.setNameIndex(i);
    }

    public Code getCode() {
        int i = getCodeIndex();
        Attribute attribute = getAttribute(i);
        Code code = (Code) attribute;
        return code;
    }

    public void setCode(Code code) {
        atts.setElementAt(code, code_index);
    }

    public int getExceptionIndex() {
        return exception_index;
    }

    public void setExceptionIndex(int i) {
        exception_index = i;
    }

    public int getExceptionNameIndex() {
        return exceptionname_index;
    }

    public void setExceptionNameIndex(int i) {
        Exceptions exceptions = getExceptions();
        exceptions.setNameIndex(i);
    }

    public Exceptions getExceptions() {
        int i = getExceptionIndex();
        Attribute attribute = getAttribute(i);
        Exceptions exceptions = (Exceptions) attribute;
        return exceptions;
    }

    public void setExceptions(Exceptions exceptions) {
        atts.setElementAt(exceptions, exception_index);
    }

    public Attribute getAttribute(int i) {
        return (Attribute) atts.elementAt(i);
    }

    public Enumeration getAttributes() {
        return atts.elements();
    }

    public boolean removeAttribute(Attribute attribute) {
        boolean flag = atts.removeElement(attribute);
        atts.trimToSize();
        return flag;
    }
}